package com.owen.java.demo.database.h2;

import java.sql.*;

public class H2Application {

    public static void test() throws SQLException, ClassNotFoundException {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
        Statement stmt = conn.createStatement();
        stmt.execute("DROP TABLE MY_USER IF EXISTS"); //删除已经存在的表
        stmt.execute("CREATE TABLE MY_USER(ID VARCHAR(10) PRIMARY KEY,NAME VARCHAR(50))"); //创建表
        stmt.executeUpdate("INSERT INTO MY_USER VALUES('001','刘备')"); //插入数据
        stmt.executeUpdate("INSERT INTO MY_USER VALUES('002','关羽')");
        stmt.executeUpdate("INSERT INTO MY_USER VALUES('003','张飞')");

//        ResultSet rs = stmt.executeQuery("SELECT NAME FROM MY_USER WHERE ID='001'"); //查询
        ResultSet rs = stmt.executeQuery("SELECT NAME FROM MY_USER"); //查询
        while(rs.next()){
            String name = rs.getString("NAME");
            System.out.println(name);
        }
        rs.close();
        stmt.close();
        conn.close();
    }

    public static void main(String[] args) {
        try {
            test();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
